﻿// -----------------------------------------------------------------------
// <copyright file="AuthorizeData.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.COMMON.ExceptionHelpers;
    using VDC.VONE.ENTITIES;
    
    /// <summary>
    /// Class access to db Authorize
    /// </summary>
    public class AuthorizeData
    {
        /// <summary>
        /// Get Authorize By Id
        /// </summary>
        /// <param name="authorizeId">Param authorizeId</param>
        /// <returns>Return Authorize</returns>
        public AuthorizeEN GetAuthorizeById(int authorizeId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                AuthorizeEN authorize = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneAuthorizeGetById, authorizeId);
                while (reader.Read())
                {
                    authorize = new AuthorizeEN();

                    if (!reader.IsDBNull(0))
                    {
                        authorize.AuthorizeId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        authorize.AuthorizeUserId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        authorize.AuthorizeBeenUserId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        authorize.DepartmentId = reader.GetInt32(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        authorize.PositionId = reader.GetInt32(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        authorize.Date = reader.GetDateTime(5);
                    }

                    if (!reader.IsDBNull(6))
                    {
                        authorize.StartDate = reader.GetDateTime(6);
                    }

                    if (!reader.IsDBNull(7))
                    {
                        authorize.EndDate = reader.GetDateTime(7);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return authorize;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Gets the authorize by authorize user id.
        /// </summary>
        /// <param name="authorizeUserId">The authorize user id.</param>
        /// <returns>return lstAuthorize;</returns>
        /// <exception cref="System.Exception">SqlException ex</exception>
        public IList<AuthorizeEN> GetAuthorizeByAuthorizeUserId(int authorizeUserId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<AuthorizeEN> lstAuthorize = new List<AuthorizeEN>();
                AuthorizeEN authorize = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneAuthorizeGetByAuthorizeUserId, authorizeUserId);
                while (reader.Read())
                {
                    authorize = new AuthorizeEN();

                    if (!reader.IsDBNull(0))
                    {
                        authorize.AuthorizeId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        authorize.AuthorizeUserId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        authorize.AuthorizeBeenUserId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        authorize.DepartmentId = reader.GetInt32(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        authorize.PositionId = reader.GetInt32(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        authorize.Date = reader.GetDateTime(5);
                    }

                    if (!reader.IsDBNull(6))
                    {
                        authorize.StartDate = reader.GetDateTime(6);
                    }

                    if (!reader.IsDBNull(7))
                    {
                        authorize.EndDate = reader.GetDateTime(7);
                    }

                    lstAuthorize.Add(authorize);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstAuthorize;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all Authorize
        /// </summary>
        /// <returns>Return list all Authorize</returns>
        public IList<AuthorizeEN> GetAuthorizes()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<AuthorizeEN> lstAuthorize = new List<AuthorizeEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneAuthorizeGetAll);
                while (reader.Read())
                {
                    AuthorizeEN authorize = new AuthorizeEN();

                    if (!reader.IsDBNull(0))
                    {
                        authorize.AuthorizeId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        authorize.AuthorizeUserId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        authorize.AuthorizeBeenUserId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        authorize.DepartmentId = reader.GetInt32(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        authorize.PositionId = reader.GetInt32(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        authorize.Date = reader.GetDateTime(5);
                    }

                    if (!reader.IsDBNull(6))
                    {
                        authorize.StartDate = reader.GetDateTime(6);
                    }

                    if (!reader.IsDBNull(7))
                    {
                        authorize.EndDate = reader.GetDateTime(7);
                    }

                    lstAuthorize.Add(authorize);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstAuthorize;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Insert to db Authorize
        /// </summary>
        /// <param name="authorize">Paarm Authorize</param>
        /// <returns>Insert Authorize</returns>
        public int Insert(AuthorizeEN authorize)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.Authorize.AuthorizeUserId, authorize.AuthorizeUserId),
                                    new SqlParameter(Constants.SqlParamerterName.Authorize.AuthorizeBeenUserId, authorize.AuthorizeBeenUserId),
                                    new SqlParameter(Constants.SqlParamerterName.Authorize.DepartmentId, authorize.DepartmentId),
                                    new SqlParameter(Constants.SqlParamerterName.Authorize.PositionsId, authorize.PositionId),
                                    new SqlParameter(Constants.SqlParamerterName.Authorize.Date, (object)authorize.Date ?? DBNull.Value),
                                    new SqlParameter(Constants.SqlParamerterName.Authorize.StartDate, (object)authorize.StartDate ?? DBNull.Value),
                                    new SqlParameter(Constants.SqlParamerterName.Authorize.EndDate, (object)authorize.EndDate ?? DBNull.Value)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneAuthorizeInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Update to db Authorize
        /// </summary>
        /// <param name="authorize">Param Authorize</param>
        public void Update(AuthorizeEN authorize)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.Authorize.AuthorizeId, authorize.AuthorizeId),
                                    new SqlParameter(Constants.SqlParamerterName.Authorize.AuthorizeUserId, authorize.AuthorizeUserId),
                                    new SqlParameter(Constants.SqlParamerterName.Authorize.AuthorizeBeenUserId, authorize.AuthorizeBeenUserId),
                                    new SqlParameter(Constants.SqlParamerterName.Authorize.DepartmentId, authorize.DepartmentId),
                                    new SqlParameter(Constants.SqlParamerterName.Authorize.PositionsId, authorize.PositionId),
                                    new SqlParameter(Constants.SqlParamerterName.Authorize.Date, (object)authorize.Date ?? DBNull.Value),
                                    new SqlParameter(Constants.SqlParamerterName.Authorize.StartDate, (object)authorize.StartDate ?? DBNull.Value),
                                    new SqlParameter(Constants.SqlParamerterName.Authorize.EndDate, (object)authorize.EndDate ?? DBNull.Value)
                                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneAuthorizeUpdate, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete Authorize by authorizeId
        /// </summary>
        /// <param name="authorizeId">param authorizeId</param>
        public void Delete(int authorizeId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneAuthorizeDelete, authorizeId);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Gets the authorize by authorize user_ authorize been user.
        /// </summary>
        /// <param name="authorizeUserId">The authorize user id.</param>
        /// <param name="authorizeBeenUserId">The authorize been user id.</param>
        /// <returns>return authorize</returns>
        /// <exception cref="System.Exception">SqlException ex</exception>
        public AuthorizeEN GetAuthorizeByAuthorizeUser_AuthorizeBeenUser(int authorizeUserId, int authorizeBeenUserId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                AuthorizeEN authorize = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneAuthorizeGetByAuthorizeUserAuthorizeBeenUser, authorizeUserId, authorizeBeenUserId);
                while (reader.Read())
                {
                    authorize = new AuthorizeEN();

                    if (!reader.IsDBNull(0))
                    {
                        authorize.AuthorizeId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        authorize.AuthorizeUserId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        authorize.AuthorizeBeenUserId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        authorize.DepartmentId = reader.GetInt32(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        authorize.PositionId = reader.GetInt32(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        authorize.Date = reader.GetDateTime(5);
                    }

                    if (!reader.IsDBNull(6))
                    {
                        authorize.StartDate = reader.GetDateTime(6);
                    }

                    if (!reader.IsDBNull(7))
                    {
                        authorize.EndDate = reader.GetDateTime(7);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return authorize;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Gets the valid authorize for user.
        /// </summary>
        /// <param name="authorizeBeenUserId">The authorize been user id.</param>
        /// <returns>return AuthorizeEN</returns>
        public AuthorizeEN GetValidAuthorizeForUser(int authorizeBeenUserId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                AuthorizeEN authorize = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneValidAuthorizeGetByAuthorizeBeenUserId, authorizeBeenUserId);
                while (reader.Read())
                {
                    authorize = new AuthorizeEN();

                    if (!reader.IsDBNull(0))
                    {
                        authorize.AuthorizeId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        authorize.AuthorizeUserId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        authorize.AuthorizeBeenUserId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        authorize.DepartmentId = reader.GetInt32(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        authorize.PositionId = reader.GetInt32(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        authorize.Date = reader.GetDateTime(5);
                    }

                    if (!reader.IsDBNull(6))
                    {
                        authorize.StartDate = reader.GetDateTime(6);
                    }

                    if (!reader.IsDBNull(7))
                    {
                        authorize.EndDate = reader.GetDateTime(7);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return authorize;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Check exit authorize when add new or update
        /// </summary>
        /// <param name="authorizeId">param authorizeId</param>
        /// <param name="authorizeBeenUserId">param authorizeBeenUserId</param>
        /// <param name="startDate">param startDate</param>
        /// <param name="endDate">param endDate</param>
        /// <returns>return true or false</returns>
        public bool Exist(int authorizeId, int authorizeBeenUserId, DateTime startDate, DateTime endDate)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneAuthorizeExist, authorizeId, authorizeBeenUserId, startDate, endDate));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result > 0;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}